package com.shengde.jinwawaforleader.model.myJob.monitoring;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hyphenate.easeui.widget.EaseSwitchButton;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProParentPerMonitoringList;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * 获取所有 班级(视频监控权限)
 * Created by Administrator on 2016/10/24 0024.
 */
public class ParentPermissionActivity extends BaseActivity implements AdapterView.OnItemClickListener {
	private static final int FINSH = 1;
	private static final String TAG = "ParentPessionActivity";
	private TextView tv_back;
	private TextView tv_title;
//	private TextView fill_add;
	private ListView lv_parent_permission_class;
	private List<ProParentPerMonitoringList.ClistBean> ClistBean;
	private String deviceSerial;

	private CommonBaseAdapter parentPermissonAdapter;
	private EaseSwitchButton ease_switch_btn_all;//所有权限开启按钮
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					if (parentPermissonAdapter == null) {
						parentPermissonAdapter = new CommonBaseAdapter(ParentPermissionActivity.this, ClistBean, R.layout.parent_permisson_activity_item) {
							@Override
							public void convert(final ViewHolder holder, int position) {
								holder.setText(R.id.tv_permission_total,"班级总共"+ClistBean.get(position).getCount()+"人");
								holder.setText(R.id.tv_class_name,ClistBean.get(position).getClassname());

					/*			final EaseSwitchButton ease_switch_btn = holder.getView(R.id.ease_switch_btn);
								ease_switch_btn.setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View v) {
										if (ease_switch_btn.isSwitchOpen()) {
											ease_switch_btn.closeSwitch();
										} else {
											ease_switch_btn.openSwitch();
										}
									}
								});*/
							}
						};

						lv_parent_permission_class.setAdapter(parentPermissonAdapter);
					} else {
						parentPermissonAdapter.notifyDataSetChanged();
					}
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.parent_permisson_activity);
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
//		fill_add = (TextView) findViewById(R.id.fill_add);
		lv_parent_permission_class = (ListView) findViewById(R.id.lv_parent_permission_class);
		ease_switch_btn_all = (EaseSwitchButton) findViewById(R.id.ease_switch_btn_all);
	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.class_permission));
//		fill_add.setText(getString(R.string.save));
		deviceSerial = getIntent().getStringExtra("deviceSerial");

		showClassList();
	}

	/**
	 * 网络请求获取 班级列表
	 */
	private void showClassList() {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/camera/cameraLookcalss";
		} else {
			url = BaseProtocol.HOST_BASE + "/camera/cameraLookcalss";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("schoolid", User.getSchoolId(this));
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProParentPerMonitoringList pro_monitoring_list = new Gson().fromJson(result, ProParentPerMonitoringList.class);
				Log.i(TAG, "onSuccess: code" + pro_monitoring_list.getCode());
				if (pro_monitoring_list != null && pro_monitoring_list.getCode() == 0) {
					ClistBean = pro_monitoring_list.getClist();
					Log.i(TAG, "onSuccess: beanSize" + ClistBean.size());
					mHandler.sendEmptyMessage(FINSH);
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				Log.i(TAG, "onError: ex" + ex);
			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
//		fill_add.setOnClickListener(this);
		ease_switch_btn_all.setOnClickListener(this);
		lv_parent_permission_class.setOnItemClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
		/*	case R.id.fill_add:
				showToastShort("修改成功");
				finish();
				break;
			case R.id.ease_switch_btn_all:  //开启所有教师权限按钮
				if (ease_switch_btn_all.isSwitchOpen()) {
					ease_switch_btn_all.closeSwitch();
				} else {
					ease_switch_btn_all.openSwitch();
				}
				break;*/
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(this, ParentPermissionDetiailActivity.class);
		intent.putExtra("deviceSerial", deviceSerial);
		intent.putExtra("className",ClistBean.get(position).getClassname());
		intent.putExtra("classid",ClistBean.get(position).getClassid());
		startActivity(intent);
	}
}
